package belf.migrate.engine.job.structure.helper;

import java.util.ArrayList;
import java.util.List;

/**
 * 表的引用关系，当前表被哪些外部表引用
 */
public class TableRelation {
    private String tableName;

    /**
     * 当前表被哪些表引用
     */
    private List<String> referredTables = new ArrayList<>();

    public TableRelation(String tableName) {
        this.tableName = tableName;
    }

    public void addReffered(String tableName) {
        referredTables.add(tableName);
    }

    public void removeReffered(String tableName) {
        referredTables.remove(tableName);
    }

    /**
     * 当前表是否可以删除？没有被外部依赖，即可删除当前表
     * @return true表示可删除，false表示无法删除
     */
    public boolean canDrop() {
        return referredTables.isEmpty();
    }

    @Override
    public String toString() {
        return tableName + " referredBy " + referredTables + '}';
    }
}
